package andestours.interfaz;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import andestours.mundo.AndesTours;
import andestours.mundo.DestinoValue;
import andestours.mundo.ServicioValue;
import andestours.mundo.TransporteValue;


public class ServletCompra extends ServletTemplate {

	private static final long serialVersionUID = 1L;
	private String codigoReserva;
	private boolean consulto;
	private boolean compro;
	private boolean exito;
	private String costo;
	private DestinoValue destino;
	private ServicioValue servicio;
	private TransporteValue transporte;

	@Override
	public void imprimirContenido(HttpServletRequest request, PrintWriter out) {
		
		if(request.getParameter("reserva") != null) 
			codigoReserva = request.getParameter("reserva");
		else
			codigoReserva = "";
		
		consulto = request.getParameter("consulto") != null;
		compro = request.getParameter("compro") != null;
		
		if(consulto) {
			ArrayList<String> datos;
			try {
				datos = AndesTours.getInstance().darReserva(Integer.parseInt(codigoReserva));
				destino = AndesTours.getInstance().darDestino(Integer.parseInt(datos.get(0)));
				servicio = AndesTours.getInstance().darServicio(datos.get(1));
				transporte = AndesTours.getInstance().darTransporte(Integer.parseInt(datos.get(2)));
				costo = String.valueOf(servicio.getCostoH() + servicio.getCostoR() + transporte.getCosto());
			} catch (Exception e) {
				consulto = false;
			}
		}
		
		if(compro) {
			try {
				Date inicio = new Date();
				AndesTours.getInstance().comprar(Integer.parseInt(codigoReserva));
				Date fin = new Date();
				System.out.println("RF5 Comprar: " + (fin.getTime() - inicio.getTime()) + " milisegundos.");
				exito = true;
			} catch (Exception e) {
				exito = false;
			}
		}
			
		
		out.println("    <tr> ");
		out.println("        <td width='35%' bgcolor='#6666aa' valign='baseline'><center> ");
		out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#669933'><b>HACER UNA COMPRA</b></font><br /> ");
		out.println("                <br /> ");
		out.println("                <br /> ");
		out.println("                <br /> ");
		out.println("                <br /> ");
		out.println("                <form action='compra.html' method='post'> ");
		out.println("                   <font face='Verdana, Arial, Helvetica, sans-serif'>C&oacute;digo de reserva<br/>");
		out.println("					<input type='text' size='4' maxlength='5' id='reserva' name='reserva' value='" + codigoReserva + "' />");
		out.println("					<input type='hidden' id='reserva' name='reserva' value='" + codigoReserva + "' />");
		out.println("					<input type='hidden' id='consulto' name='consulto' value='consulto' />");
		out.println("				</font><br /><br /><br />");
		out.println("					<input type='submit' value='Buscar'></form><br /><br /><br /><br />");
		out.println("                <form action='compra.html' method='post'> ");
		out.println("					<input type='hidden' id='compro' name='compro' value='compro' />");
		out.println("					<input type='hidden' id='reserva' name='reserva' value='" + codigoReserva + "' />");
		if(consulto)
			out.println("					<font face='Verdana, Arial, Helvetica, sans-serif'>Costo total: $ " + costo + " / d&iacute;a</font>");
		out.println("					<br /><input type='submit' value='COMPRAR' " + (consulto ? "" : "disabled='disabled'") + " /> ");
		out.println("                </form> ");
		out.println("            </center></td> ");
		out.println("        <td valign='baseline'><center>");
		if(!consulto)
			out.println("<font face ='Verdana, Arial, Helvetica, sans-serif'>" + (compro ? exito ? "Su compra fue realizada con &eacute;xito.<br />Buen viaje!" : "Su compra fue rechazada por sobrecupo.<br>Por favor cambie su selecci&oacute;n y ensaye de nuevo." : "<br /><br />") + "</font><br /><img src='./compra.jpg' height='350' /> ");
		else {
			out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>" + destino.getNombre() + "</font> <br /> ");
			out.println("                <font face='Times New Roman, Times, serif'>" + destino.getPais() + " - " + destino.getContinente() + "</font> <br /> ");
			out.println("                <img border='2' height='150' src='" + destino.getFoto() + "' /> <br /> ");
			out.println("                <br /><br /> ");
			out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>" + servicio.getNombre() + "</font> <br />");
			for(int i=0; i<servicio.getTipo(); i++)
				out.println("&#10029;");
			out.println("				 <br />Restaurante " + servicio.getRestaurante());
			out.println("				 <br /><br /><br />");
			out.println(" 			<font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>" + transporte.getEmpresa() + "</font> ");
			out.println(" 			<br />Tipo: " + transporte.getTipo());
			out.println(" 			<br />N&uacute;mero: " + transporte.getNum_Viaje());
		}
		out.println("            </center> ");
		out.println("            </td> ");
		out.println("    </tr> ");
	}

}
